<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>替换页面元素</title>
</head>
<body>
<div id="parentDiv">
    <p>我是第一行内容</p>
    <p>我是第二行内容</p>
    <p>我是第三行内容</p>
    <p>我是第四行内容</p>
</div>
<button id="replaceButton">将第2行内容替换为JavaScript课程</button>

<script>
    var parentDiv = document.getElementById("parentDiv");
    document.getElementById("replaceButton").addEventListener("click", replaceNode);
    function replaceNode() {
        var secondChild = parentDiv.getElementsByTagName("p")[1];
        var newDiv = document.createElement("div");
        var newText = document.createTextNode("JavaScript课程 ");
        newDiv.appendChild(newText);
        parentDiv.replaceChild(newDiv, secondChild);
    }
</script>
</body>
</html>